package com.xh.bussiness.teachbook.controller;

import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xh.bussiness.teachbook.bean.request.WrongQuestionsRequest;
import com.xh.bussiness.teachbook.model.WrongQuestions;
import com.xh.bussiness.teachbook.service.IWrongQuestionsService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhExportHeaderBean;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import com.xh.core.util.CacheUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 错题本 前端控制器
 * </p>
 *
 * @author JQM
 * @since 2019-08-27
 */
@Api(tags="错题本")
@RestController
@RequestMapping("/admin/wrongQuestions")
public class WrongQuestionsController extends BaseController {

    @Autowired
    private IWrongQuestionsService wrongQuestionsService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public XhPage queryPage(XhPage page, WrongQuestionsRequest bean){
        page=wrongQuestionsService.queryWrongQuestionsPage(page,bean);
        return page;
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(WrongQuestionsRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",wrongQuestionsService.queryWrongQuestions(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(WrongQuestionsRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=wrongQuestionsService.exportAll(request,exportHeaderBeans,bean,"queryWrongQuestionsPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param wrongQuestions 实体对象
     * @return
     */
    @ApiOperation(value="错题新增", notes = "错题新增")
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated WrongQuestions wrongQuestions){
        if(wrongQuestions.getId()==null){
            wrongQuestionsService.insert(wrongQuestions);
        }else{
            wrongQuestionsService.update(wrongQuestions);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        wrongQuestionsService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        wrongQuestionsService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
    
    /**
     * 批量删除操作
     * @param token
     * @return
     */
    @PostMapping("/queryCountBySubject")
    @Transactional
    public AjaxResult queryCountBySubject(String token){
        String stuId = (String) CacheUtils.get(token);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, wrongQuestionsService.queryCountBySubject(stuId));
    }
    
    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @ApiOperation(value="根据条件查询", notes = "根据学科 ，错题类型查询 ")
    @PostMapping("/queryBySubject")
    public AjaxResult queryBySubject(WrongQuestionsRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",wrongQuestionsService.queryBySubject(bean));
    }
    
    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryByPage")
    @ApiOperation(value="分页查询", notes = "分页查询，返回数据中包含题目信息 ")
    public XhPage queryByPage(XhPage page, WrongQuestionsRequest bean){
        page=wrongQuestionsService.queryByPage(page,bean);
        return page;
    }
    
    /**
     * 查询每个知识点的错题个数
     * @param bean
     * @return
     */
    @PostMapping("/queryCountByKnowledge")
    public AjaxResult queryCountKnowledge(WrongQuestionsRequest bean){
    	 return new AjaxResult(AjaxResultConstant.Result.SUCCESS,wrongQuestionsService.queryCountKnowledge(bean));
    }
    
    /**
     * 查询每个知识点的错题个数
     * @param bean
     * @return
     */
    @PostMapping("/queryCountByKnowledgeTree")
    public AjaxResult queryCountByKnowledgeTree(WrongQuestionsRequest bean){
    	 return new AjaxResult(AjaxResultConstant.Result.SUCCESS,wrongQuestionsService.queryCountByKnowledgeTree(bean));
    }
}

